/**
 * 
 */
package com.scpii.universal.struts.manage;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.FavoritesDto;
import com.scpii.universal.hibernate.entity.FavoritesTab;
import com.scpii.universal.service.FavoritesManageService;
import com.scpii.universal.service.FavoritesService;
import com.scpii.universal.service.TypeService;

/**
 * universalWeb
 * 
 * @author weny 2012-7-9下午05:20:49
 */
@Controller
@Scope("prototype")
public class FavoritesAction extends BaseAction {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4629317011625914415L;
	@Resource
	private FavoritesManageService favoritesManageService;
	@Resource
	private FavoritesService favoritesService;
	@Resource
	private TypeService typeService;

	// 分享类型
	private Map<Integer, String> typeTabMap;

	private Pagination page = new Pagination();

	private List<FavoritesDto> favoritesList ;

	private FavoritesDto favoritesInfo ;

	private FavoritesDto condition ;

	public String getFavoritesPage() {
		// modify by zhangqiang 20120725
		// UserInfoDto userInfoDto=(UserInfoDto)
		// session.getAttribute("userInfoDto");
		if(condition==null){
			condition=new FavoritesDto();
		}
		condition.setIntAppId(getUserInfo().getAppId());
		typeTabMap = typeService
				.getTypeListByParentId(GenericConstants.FAVOURT_TYPE_ID);

		// 获取返回信息的条数
		Integer recordCount = favoritesManageService
				.getFavoritesRecode(condition);

		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(recordCount);
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");
		// 获取页面返回list
		favoritesList = favoritesManageService.getFavoritesListPage(condition,
				page.getCurrentPage(), page.getPageStart(),
				page.getPageRecord());
		
		for(FavoritesDto favoritesDto:favoritesList){
			favoritesDto.setVarTypeName(typeTabMap.get((int)favoritesDto.getIntType()));
		}

		return SUCCESS;
	}

	/**
	 * 获取分享信息
	 * 
	 * @return
	 * @author weny 2012-7-13上午10:00:26
	 */
	public String getFavorites() {
		String favoritesId = request.getParameter("favoritesId");
		favoritesInfo = favoritesManageService.getFavoritesInfo(Integer
				.valueOf(favoritesId));
		return SUCCESS;
	}

	/**
	 * 删除分享信息
	 * 
	 * @return
	 * @author weny 2012-7-19下午03:06:13
	 */
	public String delFavorites() {
		String favoritesId = request.getParameter("favoritesId");
		FavoritesTab favoritesTab = favoritesService.get(Integer
				.valueOf(favoritesId));
		favoritesTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		favoritesService.update(favoritesTab);
		return SUCCESS;
	}

	// ======================get() set()方法
	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public List<FavoritesDto> getFavoritesList() {
		return favoritesList;
	}

	public void setFavoritesList(List<FavoritesDto> favoritesList) {
		this.favoritesList = favoritesList;
	}

	public FavoritesDto getFavoritesInfo() {
		return favoritesInfo;
	}

	public void setFavoritesInfo(FavoritesDto favoritesInfo) {
		this.favoritesInfo = favoritesInfo;
	}

	public Map<Integer, String> getTypeTabMap() {
		return typeTabMap;
	}

	public void setTypeTabMap(Map<Integer, String> typeTabMap) {
		this.typeTabMap = typeTabMap;
	}

	public FavoritesDto getCondition() {
		return condition;
	}

	public void setCondition(FavoritesDto condition) {
		this.condition = condition;
	}

}
